package com.jsecode.protocol.gid22;

import java.util.Calendar;
import java.util.Date;


import com.jsecode.lib.util.BigEndian;
import com.jsecode.lib.util.ByteUtil;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * gid协议中一些公用函数的封装
 *
 *
 * @author grind
 * @date Mar 28, 2014 3:35:36 PM
 * @version 1.0
 */
public class GidUtil {

    /** 
     * 读取6个字节，并组成一个long， 返回
     * @param srcBuf readIndex自动增加6
     * @return
     */
    public static long readLong6(ByteBuf srcBuf) {
        return BigEndian.getLong6(srcBuf.readBytes(6).array(), 0);
    }
    
    /** 
     * 将value按照6个字节写入srcBuf中
     * @param srcBuf writeIndex自动增加6
     * @return
     */
    public static void writeLong6(ByteBuf srcBuf, long value) {
        srcBuf.writeByte((byte)(value >>> 40));
        srcBuf.writeByte((byte) (value >>> 32));
        srcBuf.writeByte((byte) (value >>> 24));
        srcBuf.writeByte((byte) (value >>> 16));
        srcBuf.writeByte((byte) (value >>> 8));
        srcBuf.writeByte((byte) value);
    }


    /** 将6个bytes的字节转成long类型*/
    public static long getLong6(byte[] array, int index) {
        return BigEndian.getLong6(array, index);
    }

    /**
     * 字节数据转成日期
     * @param srcBuf 从readIndex起的数据, 结束后自动 readIndex增加6
     * @return 返回字符传
     */
    public static Date readDate6(ByteBuf srcBuf) {
        //    0   GPS Hour    1   u8  0 to 23
        //    1   GPS Minute  1   u8  0 to 59
        //    2   GPS Seconds 1   u8  0 to 59
        //    3   GPS Year    1   u8  0 to 99
        //    4   GPS Month   1   u8  1 to 12
        //    5   GPS Day 1   u8  1 to 31
        int hour = srcBuf.readUnsignedByte();
        int min = srcBuf.readUnsignedByte();
        int sec = srcBuf.readUnsignedByte();
        int year = 2000 + srcBuf.readUnsignedByte();
        int mon = srcBuf.readUnsignedByte() -1;
        int day = srcBuf.readUnsignedByte();

        
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, mon);
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, min);
        cal.set(Calendar.SECOND, sec);
        // above is UTC time, need +8 to CST time
        cal.add(Calendar.HOUR, 8);
        return cal.getTime();
    }
    
    /** 
     * 将日期按照6个字节写入srcBuf中
     * @param srcBuf writeIndex自动增加6
     * @return
     */
    public static void writeDate6(ByteBuf srcBuf, Calendar cal) {
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int min = cal.get(Calendar.MINUTE);
        int sec = cal.get(Calendar.SECOND);
        int year = cal.get(Calendar.YEAR) - 2000;
        int mon = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        
        srcBuf.writeByte(hour);
        srcBuf.writeByte(min);
        srcBuf.writeByte(sec);
        srcBuf.writeByte(year);
        srcBuf.writeByte(mon);
        srcBuf.writeByte(day);
    }

    public static void main(String[] args){
        String strPack = "00 00 C2 82 29 B0";
        byte[] result = ByteUtil.fromHexString(strPack, " ");
        System.out.println("origin:" + ByteUtil.formatHexStr(result));
        System.out.println("tolong:" + getLong6(result, 0));
        
        ByteBuf buf = Unpooled.wrappedBuffer(result);
        long devid = readLong6(buf);
        System.out.println("tolong:" + devid);
        
        ByteBuf buf2 = Unpooled.buffer(6);
        writeLong6(buf2, devid);
        System.out.println("origin:" + ByteUtil.formatHexStr(buf2.array()));
        
        String strDate = "0B 06 04 0E 03 1C";
        byte[] bDate = ByteUtil.fromHexString(strDate, " ");
        System.out.println("date:" + readDate6(Unpooled.wrappedBuffer(bDate)));
    }

}
